package org.example.lettuce;

import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author 宋孟林
 * @description
 * @create: 2025/3/7 20:05
 */
@Slf4j
public class LettuceDemo {
    /**
     * redis命令的返回值为OK的枚举类
     */
    private static final String MESSAGE = "OK";
    /**
     * lettuce构建过程
     * 1.使用链式调用构造uri。
     * 2根据uri创建redis客户端
     * 3.根据客户端创建一个连接对象，使用连接对象再创建命令对象
     * 4.使用命令对象来操作redis命令
     * 5.关闭资源
     * @param args args
     */
    public static void main(String[] args) {
        //使用构建器链式编程来构建builder我们的RedisURI
        RedisURI uri = RedisURI.Builder
                .redis("192.168.100.133")
                .withPort(6379)
                .withAuthentication("default", "123456")
                .build();

        RedisClient redisClient = RedisClient.create(uri);
        StatefulRedisConnection<String, String> connect = redisClient.connect();
        RedisCommands<String, String> commands = connect.sync();
        //使用连接对象创建的命令对象来操作redis命令交互
        List<String> keys = commands.keys("*");
        keys.forEach(System.out::println);

        String k5 = commands.set("k5", "hello,lettuce");
        if (MESSAGE.equals(k5)){

        }
        String k5Value = commands.get("k5");
        System.out.println("k5Value = " + k5Value);
        connect.close();
        redisClient.shutdown();
    }
}
